package com.baizhi.controller;

import com.baizhi.entity.CmfzBanner;
import com.baizhi.service.CmfzBannerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

/**
 * @Author: renpu
 * @Date: 2019/2/27 9:16
 * @Description:
 */
@Controller
@RequestMapping("/Banner")
public class CmfzBannerController {
    @Autowired
    private CmfzBannerService bannerService;

    @RequestMapping("/selectByPage")
    @ResponseBody
    public Map selectByPage(int rows,int page){
        System.out.println(rows+"****进入控制器了****"+page);
        return bannerService.selectByPage(rows,page);
    }

    @RequestMapping("/deleteMany")
    @ResponseBody
    public Map deleteMany(int []ids,Map map){
        System.out.println("进入删除控制器");
        try{
            bannerService.deleteMany(ids);
            map.put("isdele",true);
        }catch (Exception e){
            e.printStackTrace();
            map.put("isdele",false);
        }
        return map;
    }

    @RequestMapping("/add")
    public void add(CmfzBanner banner, MultipartFile mypicture, HttpSession session, Map map) throws Exception {

        //获取服务器的路径是否存在
        String p=session.getServletContext().getRealPath("/img");

        //创建文件夹名称为img
        File file=new File(p);

        //如果不存在此名称的文件夹
        if(!file.exists()){

            //开始创建一个文件夹
            file.mkdir();
        }
        String oldName=mypicture.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        //获取相对路径；
        String oop="/img/"+newName;

        //获取结对路径；
        String realPath=session.getServletContext().getRealPath(oop);
        System.out.println(realPath);
        mypicture.transferTo(new File(realPath));
        banner.setBannerImageUrl(oop);
        bannerService.add(banner);
        map.put("newName",newName);
    }

    @RequestMapping("/update")
    public void update(CmfzBanner banner,MultipartFile multipartFile, HttpSession session) throws Exception {
        System.out.println(banner);
        System.out.println("进入修改得控制器方法"+multipartFile);
        String p=session.getServletContext().getRealPath("/img");
        File file=new File(p);
        if(!file.exists()){
            file.mkdir();
        }
        String oldName=multipartFile.getOriginalFilename();
        String newName= UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
        //获取相对路径；
        String oop="/img/"+newName;

        //获取结对路径；
        String realPath=session.getServletContext().getRealPath(oop);
        System.out.println(realPath);
        multipartFile.transferTo(new File(realPath));
        System.out.println(oop);
        banner.setBannerImageUrl(oop);
       bannerService.update(banner);
    }

}
